from Common.Base import Base

login_url = "http://localhost:8080/ecshop/user.php"


class LoginPage(Base):
	# 封装表现出: 元素的定位器
	username_loc = ("name", "username")  # 用户名
	password_loc = ("name", "password")  # 密码
	remember_loc = ("id", "remember")  # 保存登录信息
	submit_loc = ("name", "submit")  # 立即登录
	pwd_question_loc = ("link text", "密码问题")  # 密码问题
	mail_loc = ("link text", "邮件")  # 邮件
	note_loc = ("link text", "短信验证")  # 短信验证
	home_page_loc = ("link text", "首页")  # 首页
	register_loc = ("xpath", "//a[@href='user.php?act=register']")  # 立即注册
	result_name_loc = ('class name', 'f4_b')

	# 封装操作层: 元素的具体操作
	def input_username(self, username):
		"""
		输入用户名
		:param username: 用户名
		:return:
		"""
		self.send_keys(text=username, locator=self.username_loc)

	def input_password(self, password):
		"""
		输入密码
		:param password:
		:return:
		"""
		self.send_keys(text=password, locator=self.password_loc)

	def click_remember(self):
		"""
		点击记住登录信息
		:return:
		"""
		self.click(locator=self.remember_loc)

	def click_login(self):
		"""
		点击立即登录
		:return:
		"""
		self.click(locator=self.submit_loc)

	def click_pwd_question(self):
		"""
		点击密码问题
		:return:
		"""
		self.click(locator=self.pwd_question_loc)

	def click_mail(self):
		"""
		点击邮件
		:return:
		"""
		self.click(locator=self.mail_loc)

	def click_note(self):
		"""
		点击短信验证
		:return:
		"""
		self.click(locator=self.note_loc)

	def click_home_page(self):
		"""
		点击首页
		:return:
		"""
		self.click(locator=self.home_page_loc)

	def click_register(self):
		"""
		点击立即注册
		:return:
		"""
		self.click(locator=self.register_loc)

	def is_login_success(self, username):
		"""
		判断是否登录成功
		:param username: 输入的用户名
		:return: bool
		"""
		return self.is_text_equal_in_element(text=username, locator=self.result_name_loc)


if __name__ == '__main__':
	from Common.Base import open_browser
	import time

	# 打开浏览器
	login = LoginPage(open_browser())
	# 访问登录页面
	login.open_url(login_url)
	# # 输入用户名
	# login.input_username("xiao")
	# # 输入密码
	# login.input_password("xs1326022865")
	# # 点击记住登录信息
	# login.click_remember()
	# # 点击立即登录
	# login.click_submit()

	# 点击密码问题
	login.click_pwd_question()
	time.sleep(1)
	login.back_browser()  # 后退
	# 点击邮件
	login.click_mail()
	time.sleep(1)
	login.back_browser()  # 后退
	# 点击短信验证
	login.click_note()
	time.sleep(1)
	login.back_browser()  # 后退
	# 点击首页
	login.click_home_page()
	time.sleep(1)
	login.back_browser()  # 后退
	# 点击立即注册
	login.click_register()
	time.sleep(1)
	login.back_browser()  # 后退
	# 关闭浏览器
	time.sleep(3)
	login.close_browser()
